package com.pucheng.bracelet.ui.activity.sao2

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.App
import com.pucheng.bracelet.app.base.BaseHistoryToDateActivity
import com.pucheng.bracelet.app.base.BaseHistoryToListActivity
import com.pucheng.bracelet.app.database.repository.Sao2Repository
import com.pucheng.bracelet.data.bean.HistoryListBean
import com.pucheng.bracelet.data.bean.Sao2Bean
import com.pucheng.bracelet.viewmodel.sao2.Sao2ViewModel
import com.pucheng.bracelet.viewmodel.sao2.WordViewModelFactory
import kotlinx.android.synthetic.main.activity_sao2.*
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

class Sao2HistoryActivity : BaseHistoryToListActivity<BaseViewModel, Sao2Bean>() {

    private var historyLists = mutableListOf<Sao2Bean>()
    private lateinit var mAdapter: BaseQuickAdapter<Sao2Bean,BaseViewHolder>
    private val sao2ViewModel: Sao2ViewModel by viewModels { WordViewModelFactory(Sao2Repository((application as App).database.sao2Dao())) }
    override fun titleResId(): Int = R.string.sao2_history

    override fun initView(savedInstanceState: Bundle?) {
        mAdapter = object : BaseQuickAdapter<Sao2Bean, BaseViewHolder>(R.layout.item_list_history,historyLists) {
            override fun convert(holder: BaseViewHolder, item: Sao2Bean) {
                holder.setTextColorRes(R.id.tv_history_content,R.color.sao2_bg).setText(R.id.tv_history_content, item.sao2Vaule.toString()+"%")
                    .setText(R.id.tv_history_time, item.measuredDate)
            }

        }
        super.initView(savedInstanceState)
    }

    override fun getHistoryAdapter(): BaseQuickAdapter<Sao2Bean, BaseViewHolder> = mAdapter

    override fun createObserver() {
        super.createObserver()
        sao2ViewModel.apply {

            allSao2.observe(this@Sao2HistoryActivity, Observer {
                it.let {
                    historyLists.clear()
                    historyLists.addAll(it as MutableList<Sao2Bean>)
                    mAdapter.notifyDataSetChanged()
                }
            })
        }
    }

}